package com.example.animalcase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.example.animalcase.bean.Note;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 王宝田 on 2023/6/26.
 */
public class NoteDbOpenHelper extends SQLiteOpenHelper {
    private static final String DB_NAME="noteSQLite.db";
    private static final String TABLE_NAME_NOTE="note";
    private static final String CREATE_TABLE_SQL="create table "+TABLE_NAME_NOTE +" (id integer primary key autoincrement,title text,content text,create_time text)";

    public NoteDbOpenHelper(Context context){
        super(context,DB_NAME,null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_SQL);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public long insertDaate(Note note){
        SQLiteDatabase db=getWritableDatabase();

        ContentValues values=new ContentValues();
        values.put("title",note.getTitle());
        values.put("content",note.getContent());
        values.put("create_time",note.getCreatedTime());

        return db.insert(TABLE_NAME_NOTE,null,values);

    }

    public int deteleFromDbById(String id){
        SQLiteDatabase db=getWritableDatabase();
        return db.delete(TABLE_NAME_NOTE,"id like ?",new String[]{id});
    }


    public int updateDate(Note note){
        SQLiteDatabase db =getWritableDatabase();

        ContentValues values=new ContentValues();
        values.put("title",note.getTitle());
        values.put("content",note.getContent());
        values.put("create_time",note.getCreatedTime());


        return db.update(TABLE_NAME_NOTE,values,"id like ?",new String[]{note.getId()});

    }

    public List<Note> queryAllFtomDb(){
        SQLiteDatabase db =getWritableDatabase();
        List<Note> noteList=new ArrayList<>();

        Cursor cursor=db.query(TABLE_NAME_NOTE,null,null,null,null,null,null);

        if (cursor !=null){
            while (cursor.moveToNext()){
                String id=cursor.getString(cursor.getColumnIndex("id"));
                String title=cursor.getString(cursor.getColumnIndex("title"));
                String content=cursor.getString(cursor.getColumnIndex("content"));
                String createTime=cursor.getString(cursor.getColumnIndex("create_time"));

                Note note=new Note();
                note.setId(id);
                note.setTitle(title);
                note.setContent(content);
                note.setCreatedTime(createTime);

                noteList.add(note);
            }
            cursor.close();
        }
        return noteList;
    }


}
